package yxk.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.IFormView;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.hr.hom.formplugin.web.personmange.OnbrdConfirmSingleEdit;
import kd.sdk.plugin.Plugin;
import yxk.list.odc.homs.util.OnboardUtil;

import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态表单 单个确认入职 点击确认按钮时调用第三方接口
 */
public class OnbrdConfirmSingleEditFormPlugin extends AbstractFormPlugin implements Plugin {

    private static final Log LOGGER = LogFactory.getLog(OnbrdConfirmSingleEditFormPlugin.class);
    private static final String BTN_CONFIRM = "btn_confirm";
    private OnboardUtil onboardUtil=new OnboardUtil();
    public void registerListener(EventObject eventObject) {
        super.registerListener(eventObject);
        this.addClickListeners("btn_confirm");
    }

    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        if (evt.getSource() instanceof Control) {
            Control control = (Control) evt.getSource();
            String key = control.getKey();
            if (BTN_CONFIRM.equals(key)) {

                    DynamicObject simpleDynamicObject = this.getModel().getDataEntity();
                    Long id = simpleDynamicObject.getLong("id");

                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(
                            "hom_onbrdinfo", new QFilter[]{new QFilter("id", QCP.equals, id)});
                String offerNumber = dynamicObject.getString("offernumber");
                //offer不为空时为摩卡系统创建的入职单
                if(offerNumber != null && !"".equals(offerNumber)) {
                        String candidatenumber = dynamicObject.getString("candidatenumber");//候选人编码  employeeno工号
                        Integer probationtime = dynamicObject.getInt("probationtime");//试用期
                        //String offerNumber = dynamicObject.getString("offernumber");//offer编号
                        Date effectdate = dynamicObject.getDate("effectdate");

                        String token = onboardUtil.getZhaoPinToken();

                        Map<String, Object> result = onboardUtil.getHired(token, candidatenumber, probationtime, effectdate);

                        //Map<String, Object> result = OnboardUtil.getHired(token, candidatenumber, probationtime);
                        Boolean isSuccess = (Boolean) result.get("success");
                        if (!isSuccess) {
                            String errorMessage = (String) result.get("errorMessage");
                            this.getView().showMessage(errorMessage);
                            evt.setCancel(true);
                        } else {
                            this.getView().showMessage("入职成功");
                        }
                        LOGGER.info("result:", result);
                    }
            }
        }

    }

    public void click(EventObject evt) {
        super.click(evt);
        Object object = evt.getSource();
        if (object instanceof Button) {
            Button button = (Button) object;
            String key = button.getKey();
            /*if ("btn_confirm".equals(key) && this.validateDataWithDB() && this.validateDate()) {
                this.doOnbrdConfirm();
            }*/
        }

    }
}
